package mn.more.wits.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import mn.more.wits.client.dto.ChangePasswordDTO;
import mn.more.wits.client.dto.Payload;
import mn.more.wits.client.gui.WitsButton;
import mn.more.wits.client.util.ClientModelUtil;
import mn.more.wits.client.util.InputValidator;
import net.mygwt.ui.client.Events;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.Listener;
import net.mygwt.ui.client.widget.ContentPanel;
import net.mygwt.ui.client.widget.Info;
import net.mygwt.ui.client.widget.layout.TableLayout;

/** User: nasanjargal */
public class ChangePasswordPanel extends ContentPanel {

	private static final AppMsg APP_MSG = AppMsg.Self.get();

	private Label lblOldPassword = new Label(APP_MSG.oldPassword());
	private Label lblNewPassword = new Label(APP_MSG.newPassword());
	private Label lblConfirmPassword = new Label(APP_MSG.confirmPassword());

	private PasswordTextBox tbOldPassword = new PasswordTextBox();

	private PasswordTextBox tbNewPassword = new PasswordTextBox();
	private PasswordTextBox tbConfirmPassword = new PasswordTextBox();

	private WitsButton btnChange = new WitsButton(APP_MSG.btnChange());
	private WitsButton btnCancel = new WitsButton(APP_MSG.btnCancel());

	public ChangePasswordPanel() {

		setText(APP_MSG.changePasswordText());

		setLayout(new TableLayout(2));

		lblOldPassword.setStyleName("lbl-password");
		tbOldPassword.setStyleName("tb-password");

		add(lblOldPassword);
		add(tbOldPassword);

		lblNewPassword.setStyleName("lbl-password");
		tbNewPassword.setStyleName("tb-password");

		add(lblNewPassword);
		add(tbNewPassword);

		lblConfirmPassword.setStyleName("lbl-password");
		tbConfirmPassword.setStyleName("tb-password");

		add(lblConfirmPassword);
		add(tbConfirmPassword);

		add(btnChange);
		add(btnCancel);

		btnCancel.addListener(Events.Click, new Listener() {
			public void handleEvent(BaseEvent be) {
				backPanel();
			}
		});

		btnChange.addListener(Events.Click, new Listener() {
			public void handleEvent(BaseEvent be) {

				// todo-nasaa: call rpc to change password

				final String title = APP_MSG.errorTitle();

				String oldPassword = tbOldPassword.getText();
				String newPassword = tbNewPassword.getText();
				String confirmPassword = tbConfirmPassword.getText();

				if (InputValidator.isValidPassword(oldPassword)) {
					if (InputValidator.isValidPassword(newPassword) &&
					    InputValidator.isValidPassword(confirmPassword)) {
						if (newPassword.equals(confirmPassword)) {
							ChangePasswordDTO changePassword = new ChangePasswordDTO();
							changePassword.setCurrentPassword(oldPassword);
							changePassword.setNewPassword(newPassword);

							AuthProvider.App.getInstance().changePassword(ClientModelUtil.toPayload(changePassword),
							                                              new AsyncCallback() {
								                                              public void onFailure(Throwable caught) {
									                                              Info.show(title,
									                                                        APP_MSG.msgFailureChangePassword(),
									                                                        "");
								                                              }

								                                              public void onSuccess(Object result) {
									                                              Info.show(title,
									                                                        APP_MSG.msgSuccessChangePassword(),
									                                                        "");
								                                              }
							                                              });

							Payload payload = new Payload();
						} else {
							Info.show(title, APP_MSG.msgNewPasswordNotSame(), "");
						}
					} else {
						Info.show(title, APP_MSG.msgNewPasswordInvalid(), "");
					}
				} else {
					Info.show(title, APP_MSG.msgWrongOldPassword(), "");
				}
			}
		});
	}

	private void backPanel() {
		AppContext.showStartPage();
	}
}
